package models

import (
	"errors"
	"github.com/astaxie/beego/orm"
	"object/go_cms/valid"
	"strconv"
	"time"
)

type MenuModels struct {
}

type Menu struct {
	Id         int64  `orm:pk;auto`
	Href       string `orm:size(255);default("")`
	Title      string `orm:size(50)`
	Type       int    `orm:size(1);default(1)`
	Status     int    `orm:size(1);default(1)`
	Authopen   int    `orm:size(2);default(1)`
	Icon       string `orm:size(30);default("")`
	Pid        int64
	Sort       int   `orm:size(8);default(100)`
	Addtime    int64 `orm:size(11);default(0)`
	Zt         int
	Menustatus int
	Is_menu    int `orm:size(1);default(0)`
}

func init() {
	orm.RegisterModel(new(Menu))
}

//获取指定条目数据
func (m *MenuModels) GetMenu(maps map[string]interface{}) (Menu, error) {
	menu := Menu{}
	if maps["id"] != nil {
		menu.Id = maps["id"].(int64)
	}
	if maps["href"] != nil {
		menu.Href = maps["href"].(string)
	}

	o := orm.NewOrm()
	err := o.Read(&menu)
	return menu, err
}

//列表
func (m *MenuModels) QueryMenu(maps map[string]interface{}) ([]Menu, error) {
	var list []Menu
	ps := getQueryTable("menu")

	if maps["type"] != nil {
		ps = ps.Filter("type", maps["type"])
	}
	if maps["status"] != nil {
		ps = ps.Filter("status", maps["status"])
	}
	if maps["authopen"] != nil {
		ps = ps.Filter("authopen", maps["authopen"])
	}
	if maps["pid"] != nil {
		ps = ps.Filter("pid", maps["pid"])
	}
	if maps["is_menu"] != nil {
		ps = ps.Filter("is_menu", maps["is_menu"])
	}
	if maps["zt"] != nil {
		ps = ps.Filter("zt", maps["zt"])
	}

	_, err := ps.All(&list)
	return list, err
}

//添加菜单
func (m *MenuModels) InsertMenu(maps map[string]interface{}) (int64, error) {
	b, str := valid.ValidAddMenu(maps)
	if !b {
		return 0, errors.New(str)
	}
	menu := Menu{}
	/*menu.Href = maps["href"].(string)
	menu.Title = maps["title"].(string)
	menu.Type = maps["type"].(int8)
	menu.Status = maps["status"].(int8)
	menu.Authopen = maps["authopen"].(int8)
	menu.Icon = maps["icon"].(string)
	menu.Pid = maps["pid"].(int64)
	menu.Sort = maps["sort"].(int)
	menu.Zt = maps["zt"].(int)
	menu.Menustatus = maps["menustatus"].(int8)
	menu.Is_menu = maps["is_menu"].(int8)*/
	if maps["href"] != nil {
		menu.Href = maps["href"].(string)
	}
	if maps["title"] != nil {
		menu.Title = maps["title"].(string)
	}
	if maps["type"] != nil {
		menu.Type, _ = strconv.Atoi(maps["type"].(string))
	}
	if maps["status"] != nil {
		menu.Status, _ = strconv.Atoi(maps["status"].(string))
	}
	if maps["authopen"] != nil {
		menu.Authopen, _ = strconv.Atoi(maps["authopen"].(string))
	}
	if maps["icon"] != nil {
		menu.Icon = maps["icon"].(string)
	}
	if maps["pid"] != nil {
		menu.Pid, _ = strconv.ParseInt(maps["pid"].(string), 10, 64)
	}
	if maps["sort"] != nil {
		menu.Sort, _ = strconv.Atoi(maps["sort"].(string))
	} else {
		menu.Sort = 100
	}
	if maps["zt"] != nil {
		menu.Zt, _ = strconv.Atoi(maps["zt"].(string))
	}
	if maps["menustatus"] != nil {
		menu.Menustatus, _ = strconv.Atoi(maps["menustatus"].(string))
	} else {
		menu.Menustatus = 0
	}
	if maps["is_menu"] != nil {
		menu.Is_menu, _ = strconv.Atoi(maps["is_menu"].(string))
	} else {
		menu.Is_menu = 0
	}

	menu.Addtime = time.Now().Unix()

	o := orm.NewOrm()
	return o.Insert(&menu)
}

//修改菜单
func (m *MenuModels) UpdateMenu(maps map[string]interface{}) (int64, error) {
	menu := Menu{}
	menu.Id, _ = strconv.ParseInt(maps["id"].(string), 10, 64)

	o := orm.NewOrm()
	_ = o.Read(&menu)

	if maps["href"] != nil {
		menu.Href = maps["href"].(string)
	}
	if maps["title"] != nil {
		menu.Title = maps["title"].(string)
	}
	if maps["type"] != nil {
		menu.Type, _ = strconv.Atoi(maps["type"].(string))
	}
	if maps["status"] != nil {
		menu.Status, _ = strconv.Atoi(maps["status"].(string))
	}
	if maps["authopen"] != nil {
		menu.Authopen, _ = strconv.Atoi(maps["authopen"].(string))
	}
	if maps["icon"] != nil {
		menu.Icon = maps["icon"].(string)
	}
	if maps["pid"] != nil {
		menu.Pid, _ = strconv.ParseInt(maps["pid"].(string), 10, 64)
	}
	if maps["sort"] != nil {
		menu.Sort, _ = strconv.Atoi(maps["sort"].(string))
	}
	if maps["zt"] != nil {
		menu.Zt, _ = strconv.Atoi(maps["zt"].(string))
	}
	if maps["menustatus"] != nil {
		menu.Menustatus, _ = strconv.Atoi(maps["menustatus"].(string))
	}
	if maps["is_menu"] != nil {
		menu.Is_menu, _ = strconv.Atoi(maps["is_menu"].(string))
	}
	return o.Update(&menu)
}

//删除
func (m *MenuModels) DeleteMenu(id int64) (int64, error) {
	ps := getQueryTable("menu")
	if ps.Filter("pid", id).Exist() {
		return 0, errors.New("存在子条目")
	}
	o := orm.NewOrm()
	return o.Delete(&Menu{Id: id}, "Id")
}
